package thread;

import java.util.ArrayList;

public class test {
    public static void main(String[] args) {
        ArrayList<Integer> list = new ArrayList<>();

        Thread t1 = new Thread() {
            public void run() {
                for (int i = 0; i < 1000; i++) {
                   // synchronized (list) {//同步监视器对象通常可以选取临界资源（多个线程抢的对象）
                        list.add(i);
                        System.out.println("t1ok");
                    //}
                }

            }
        };


        Thread t2 = new Thread() {
            public void run() {
                for (int i = 0; i < 1000; i++) {
                    synchronized (list) {
                        list.add(i);
                        System.out.println("t2ok");
                    }
                }

            }
        };
        t1.start();
        t2.start();


        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(list.size());
    }
}
